<template>
  <el-dialog :title="dataForm.state?(!dataForm.id ? $t('Common.Add') : $t('Common.Edit')):'详情'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <!--    <el-form-item label="用户id" prop="sysUserId">
      <el-input v-model="dataForm.sysUserId" placeholder="用户id"></el-input>
    </el-form-item> -->
      <el-form-item label="发送批次" prop="batchNumber">
        <template slot-scope="scope">
          <span>
            <el-tag>{{dataForm.batchNumber}}</el-tag>
          </span>
        </template>
      </el-form-item>
      <!--    <el-form-item label="对象" prop="name">
      <el-input v-model="dataForm.name" placeholder="对象"></el-input>
    </el-form-item> -->
      <el-form-item label="发送号码" prop="phone">
        <template slot-scope="scope">
          <span>
            <el-tag>{{dataForm.phone}}</el-tag>
          </span>
        </template>
      </el-form-item>
      <el-form-item label="发送时间" prop="creatTime">
        <template slot-scope="scope">
          <span>
            <el-tag>{{dataForm.creatTime}}</el-tag>
          </span>
        </template>
      </el-form-item>
      <el-form-item label="发送内容" prop="content">
        <el-input type="textarea" v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="发送状态" prop="state">
        <template slot-scope="scope">
          <span v-html="formtState(dataForm.state)"></span>
        </template>
      </el-form-item>
      <el-form-item label="错误原因" v-show="dataForm.state == 0" prop="msg">
        <el-input type="textarea" v-model="dataForm.msg" placeholder="原因"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
      <!-- <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button> -->
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sysUserId: '',
          batchNumber: '',
          name: '',
          phone: '',
          state: 1,
          msg: '',
          content: '',
          creatTime: ''
        },
        dataRule: {},
        loading: false
      }
    },
    methods: {
      formtState(state) {
        var stratStr = '<span class="el-tag '
        var centerStr = ' el-tag--medium">'
        var endStr = '</span>'
        switch (state) {
          case 0:
            return stratStr + 'el-tag--danger'+centerStr+'发送失败'+endStr
            break;
          case 1:
            return stratStr + 'el-tag--success'+centerStr+'发送成功'+endStr
            break;
          case 2:
            return stratStr + ''+centerStr+'正在发送'+endStr
            break;
          case 3:
            return stratStr + 'el-tag--info'+centerStr+'待退款'+endStr
            break;
        }
      },
      init(id) {
        this.loading = true
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsmshistory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.sysUserId = data.tHotelSmsHistory.sysUserId
                this.dataForm.batchNumber = data.tHotelSmsHistory.batchNumber
                this.dataForm.name = data.tHotelSmsHistory.name
                this.dataForm.phone = data.tHotelSmsHistory.phone
                this.dataForm.state = data.tHotelSmsHistory.state
                this.dataForm.msg = data.tHotelSmsHistory.msg
                this.dataForm.content = data.tHotelSmsHistory.content
                this.dataForm.creatTime = data.tHotelSmsHistory.creatTime
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          this.loading = true
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsmshistory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sysUserId': this.dataForm.sysUserId,
                'batchNumber': this.dataForm.batchNumber,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'state': this.dataForm.state,
                'msg': this.dataForm.msg,
                'content': this.dataForm.content,
                'creatTime': this.dataForm.creatTime
              })
            }).then(({
              data
            }) => {
              this.loading = false
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
